
#include <QtCore/QDir>
#include <QtCore/QStringList>
#include <QtGui/QColor>

#include "imageLoader.h"

/*----------------------------------------------------------------------------*/


ImageLoader::ImageLoader()
{
}

/*----------------------------------------------------------------------------*/


ImageLoader::~ImageLoader()
{
	for ( size_t i = 0; i < m_imageArray.size(); i++ )
		delete m_imageArray[i];
	m_imageArray.clear();
}


/*----------------------------------------------------------------------------*/


bool ImageLoader::LoadBmpFilesToImages( const QString& _trainFilesDir )
{
	for ( size_t i = 0; i < m_imageArray.size(); i++ )
		delete m_imageArray[i];
	m_imageArray.clear();

	QStringList filesList = BrowseFiles( _trainFilesDir );

	for ( int i = 0; i < filesList.size(); i++ )
		m_imageArray.push_back( CreateImageFromFile( filesList[i] ) );

	return ( m_imageArray.size() > 0 );
}


/*----------------------------------------------------------------------------*/


QStringList ImageLoader::BrowseFiles( const QString& _trainFilesDir )
{
	QDir currentDir = QDir::current();
	if ( !currentDir.cd( _trainFilesDir ) )
		throw "Directory not found!";
	
	currentDir.setNameFilters( QStringList( "*.dat" ) );
	QStringList files = currentDir.entryList();
	for ( int i = 0; i < files.size(); i++ )
	{
		files[i] = currentDir.absoluteFilePath( files[i] );
	}

	return files;
}


/*----------------------------------------------------------------------------*/


CImage* ImageLoader::CreateImageFromFile( QString& _fileName )
{
	int activeOutput = _fileName.section( '/', -1 )[0].toAscii() - 65;	
	m_dataReader.readData( _fileName.toAscii().data() );
	return new CImage( m_dataReader.getAmplitudes(), activeOutput );
}


/*----------------------------------------------------------------------------*/


ImageV& ImageLoader::GetImages()
{
	return m_imageArray;
}


/*----------------------------------------------------------------------------*/

